var app = getApp(), fetchApi = require("../../../utils/httpPost.js")
, common_Js = require("../../../component/common.js");
import Dialog from '../../../component/vant/dialog/dialog';
import Toast from '../../../component/vant/toast/toast';
const message = '是否确认取消该订单？'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    states: [-1, 2],//,3
    currentTab: 0,
    orderstate:-1,
    pageSize:10,
    pageIndex: 1,
    scrollTop: 0,
    scrollHeight:0,
    hasData:false,
    orderList:[],
    loading: false,
    netWork: false,
    isLoading: true,
    visible:false,
    itemData:[],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.data.orderstate = that.data.states[0]
  },
  reload:function(){
    this.onHttpInit(this.data.orderstate)
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function (state) {
    var that = this
    fetchApi.httpPost('/property/order/list', {
      state: state,
      pageSize: that.data.pageSize,
      pageIndex: that.data.pageIndex
    }, function (res) {
      if (res.data.code == 0) {
        if (res.data.totalcount > 0) {
          console.log("订单列表数据", res)
          var orders = res.data.data.orders;
          if(orders.length > 0){
            for (var index in orders) {
              that.data.orderList.splice(that.data.orderList.length, 0, orders[index]);
            }
            that.setData({
              hasData: true,
              orderList: that.data.orderList
            })
          }
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  handleChange({ detail }) {
    var that = this
    that.setData({
      currentTab: detail.index,
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      orderList:[]
    });
    that.data.orderstate = that.data.states[detail.index]
    that.onHttpInit(that.data.orderstate)
  },
  orderDetail:function(t){
    console.log('t',t)
    var state = t.currentTarget.dataset.state
    var orderid = t.currentTarget.dataset.orderid;
    if(state == 4){
      wx.navigateTo({
        url: "/pagesProperty/pages/orderInfo/index?order_id=" + orderid
      });
    }else{
      wx.navigateTo({
        url: "/pagesProperty/pages/propertySettle/index?state=" + state + "&order_id=" + orderid
      });
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var isLogin = app.getStorageSync("isLogin");
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      this.data.pageIndex = 1;
      this.initOp()
    }
  },
  goAppraise:function(e){
    console.log('goAppraise e',e)
    var orderid = e.currentTarget.dataset.orderid
    var infoid = e.currentTarget.dataset.infoid
    wx.navigateTo({
      url: '/pages/appraise/appraise?order_id=' + orderid + '&infoid=' + infoid,
    })
  },
  openFile(e){
    console.log('openFile',e)
    var url = e.currentTarget.dataset.item.receiptFilePath;
    wx.downloadFile({
      url: url,
      success:function(res){
        var filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          fileType: 'docx',
          success(res){
            console.log("打开成功")
          },
          fail(err){
            console.log(err)
          }
        })
      }
    })
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var e = t.currentTarget.dataset.item;
    var c = [];
    c.push(e.receiptImgUrl);
    common_Js.preImg(0, c)
  },
  payMoney:function(t){
    var order_id = t.currentTarget.dataset.orderid
    // this.payMoneyOp(order_id)
    wx.navigateTo({
      url: "/pagesProperty/pages/propertySettle/index?order_id=" + order_id
    });
  },
  payMoneyOp:function(order_id){
    console.log('order_id',order_id)
    var that = this
    fetchApi.httpPost('/pay/requestPayProperty', {
      order_id: order_id,
      subject:'交物业费'
    }, function (res) {
      console.log('提交订单',res)
      if (res.data.code == 0) {
        var pay_data = res.data.data;
        var wxpayinfo = JSON.parse(res.data.data)
        console.log('pay_data',pay_data)
        if(wxpayinfo.result_code == 'SUCCESS'){
          wx.requestPayment({
            timeStamp: wxpayinfo.timeStamp,
            nonceStr: wxpayinfo.nonceStr,
            package: wxpayinfo.package,
            signType: wxpayinfo.signType,
            paySign: wxpayinfo.paySign,
            success (res) { 
              console.log('requestPayment success',res)
              wx.redirectTo({
                url: '/pages/pay/payResult/index?type=wyfee&order_id=' + order_id,
              })
            },
            fail (res) {
              console.log('requestPayment fail',res)
             }
          })
        }else if(wxpayinfo.result_code == 'NOT_NEED_PAY') {//不跳转支付
          wx.redirectTo({
            url: '/pages/pay/payResult/index?type=wyfee&order_id=' + order_id,
          })
        }
      } else {
        that.setData({ isLoading: false, loading: true, netWork: true })
      }
    }, function () {
        that.setData({  isLoading: false,  loading: true,  netWork: true, }) 
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1;
    this.initOp()
  },
  initOp:function(){
    this.setData({
      currentTab: 0,
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      orderList:[]
    })
    this.onHttpInit(this.data.orderstate)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.data.pageIndex = this.data.pageIndex + 1
    this.onHttpInit(this.data.orderstate)
  },
  cancelOrder:function(e){
    var order_id = e.currentTarget.dataset.orderid
    var that = this
    Dialog.confirm({
      title: '取消订单',
      message
    }).then(() => {
      that.onHttpCancelOrder(order_id)
    })
    .catch(() => {
      Dialog.close();
    });
  },
  onHttpCancelOrder: function (order_id){
    console.log('order_id',order_id)
    var that = this
    fetchApi.httpPost('/property/cancelOrder', {
      order_id: order_id,
    }, function (res) {
      console.log("取消订单 返回数据", res)
      if (res.data.code == 0) {
        Toast.success('取消成功');
        that.initOp()
      } else {
        Toast.fail(res.data.msg);
      }
    },function(){})
  },

})